{% extends "base.html" %}

{% block content %}
<div class="page-title">
    <h3>在下方输入要修改的员工的编号：</h3>
</div>
<div class="form-container">
    <form id="modify-employee-form">
        <label for="employee_id">员工编号（此项仅作查找之用，若想修改本项请在下方进行）：</label>
        <input type="text" id="employee_id" name="employee_id" class="input-field" required><br>
        <button type="button" class="login-button" id="search-button">查找</button>
        <div id="modify-fields" style="display:none;">
            <label for="employee_name">员工姓名：</label>
            <input type="text" id="employee_name" name="employee_name" class="input-field" required><br>
            <label for="password">密码：</label>
            <input type="text" id="password" name="password" class="input-field" required><br>
            <label for="level">级别：</label>
            <input type="number" id="level" name="level" class="input-field"><br>
            <label for="phone">电话：</label>
            <input type="text" id="phone" name="phone" class="input-field"><br>
            <label for="salary">工资：</label>
            <input type="number" id="salary" name="salary" class="input-field"><br>
            <label for="remark">备注：</label>
            <textarea id="remark" name="remark" class="input-field"></textarea><br>
            <label for="new_employee_id">新的员工编号（此项为空则保持原员工编号不变）：</label>
            <input type="text" id="new_employee_id" name="new_employee_id" class="input-field"><br>
            <div class="button-container">
                <button type="submit" class="login-button">保存</button>
                <button type="reset" class="login-button" style="background-color: grey;">重置</button>
            </div>
        </div>
    </form>
</div>

<script>
    document.getElementById('search-button').addEventListener('click', function() {
        var employeeId = document.getElementById('employee_id').value;
        if (employeeId) {
            fetch(`/api/employees/${employeeId}`)
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        alert('该员工编号不存在');
                        document.getElementById('modify-fields').style.display = 'none';
                    } else {
                        document.getElementById('employee_name').value = data.员工姓名;
                        document.getElementById('password').value = data.员工密码;
                        document.getElementById('level').value = data.员工级别;
                        document.getElementById('phone').value = data.员工电话;
                        document.getElementById('salary').value = data.员工工资;
                        document.getElementById('remark').value = data.备注;
                        document.getElementById('modify-fields').style.display = 'block';
                    }
                }).catch(error => {
                    console.error('Error:', error);
                    alert('查找员工失败');
                });
        } else {
            alert('请输入员工编号');
            document.getElementById('modify-fields').style.display = 'none';
        }
    });

    document.getElementById('modify-employee-form').addEventListener('submit', function(event) {
        event.preventDefault();
        var employeeId = document.getElementById('employee_id').value;
        var newEmployeeId = document.getElementById('new_employee_id').value;

        const formData = new FormData(this);
        const data = Object.fromEntries(formData.entries());

        if (newEmployeeId && newEmployeeId !== employeeId) {
            if (!confirm('此次修改涉及员工编号，是否确认修改？')) {
                return;
            }
            data.new_employee_id = newEmployeeId;
        }

        fetch(`/api/employees/${employeeId}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }).then(response => {
            if (response.ok) {
                alert('员工修改成功');
                this.reset();
                document.getElementById('modify-fields').style.display = 'none';
            } else {
                response.json().then(data => {
                    alert('修改员工失败: ' + data.error);
                });
            }
        }).catch(error => {
            console.error('Error:', error);
            alert('修改员工失败');
        });
    });

    document.querySelector('button[type="reset"]').addEventListener('click', function(event) {
        if (!confirm('是否确定重置已输入信息？')) {
            event.preventDefault();
        }
    });
</script>
{% endblock %}
